package chenjun.mydatabindingdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by n6 on 2016/9/30.
 */
public class LvAdapter<T> extends BaseAdapter {

    private List<T> mList;
    private int mResid;
    private int mLayoutid;
    private LayoutInflater mInflater;

    public LvAdapter(List<T> list,int resid,int layoutid,Context context){
        mInflater = LayoutInflater.from(context);
        mLayoutid = layoutid;
        mResid = resid;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void setList(List<T> list){
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewDataBinding dataBinding;
        if (view == null)
            dataBinding = DataBindingUtil.inflate(mInflater, mLayoutid, viewGroup, false);
        else
            dataBinding = DataBindingUtil.getBinding(view);
        dataBinding.setVariable(mResid,mList.get(i));
        return dataBinding.getRoot();
    }
}
